package com.banyuan.array;

import java.util.Arrays;

//这是一段有问题的代码一直显示数组索引越界的问题
//要实现的功能是将两个有序排列的数组中的元素有序放到另外的一个数组中
public class TwoArrays {
    public static void main(String[] args) {
        int[] arr1 = new int[]{1, 3, 5, 7, 9};
        int[] arr2 = new int[]{2, 4, 6, 8, 10, 12};

        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        int[] arr3 = new int[arr1.length + arr2.length];

        //选择语句当进入一个分支之后，剩余的分支就不会在执行
        while (true) {
            if (index1 < arr1.length && index2 < arr2.length) {
                if (arr1[index1] < arr2[index2]) {
                    arr3[index3] = arr1[index1];
                    index1++;
                    index3++;
//                    continue;
                } else if (arr1[index1] >= arr2[index2]) {
                    arr3[index3] = arr2[index2];
                    index2++;
                    index3++;
//                    continue;
                }
            } else if (index1 < arr1.length) {
                for (int i = index1; i < arr1.length; i++) {
                    arr3[index3] = arr1[i];
                    index3++;
                    index1++;
//                    continue;
                }
                break;
            } else if (index2 < arr2.length) {
                for (int i = index2; i < arr2.length; i++) {
                    arr3[index3] = arr2[i];
                    index3++;
                    index2++;
                }
                break;
            }
        }
        System.out.println(Arrays.toString(arr3));
//        int E, F = 2;
//        int a = E +F;E未初始化
//        System.out.println(a);

    }



}
